package com.fattyCorps.game.controller;

import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fattyCorps.common.mgr.GlobalConfig;
import com.fattyCorps.common.packet.dto.DItem;
import com.fattyCorps.game.cache.PlayerCache;
import com.fattyCorps.game.exception.game.VerifyAccTokenErrorException;
import com.fattyCorps.game.obj.cache.PlayerInfo;
import com.fattyCorps.game.obj.msg.HttpStatus;
import com.fattyCorps.game.obj.msg.SRet;
import com.fattyCorps.game.obj.msg.client.player.CGetInfo;
import com.fattyCorps.game.obj.msg.client.player.CSetLineup;
import com.fattyCorps.game.obj.msg.server.player.SCfg;
import com.fattyCorps.game.obj.msg.server.player.SGetHangAward;
import com.fattyCorps.game.obj.msg.server.player.SGetInfo;
import com.fattyCorps.game.service.player.PlayerService;
import com.fattyCorps.game.util.GameJwtUtils;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/player")
public class PlayerController {

    @Autowired
    PlayerService playerService;

    @Value("${fatty-crops.account:http://127.0.0.1:8081}")
    String accountServer;

    // 获取角色信息
    @PostMapping("/getInfo")
    public SRet getInfo(HttpServletRequest request, @RequestBody CGetInfo msg){
        // todo: 去账号服获取id
//        Integer accountId = GameJwtUtil.getPlayerIdByRequest(request);
        String url = accountServer + "/info";
        String jsonStr = JSONUtil.toJsonStr(msg);
        Integer accountId = 0;

        try {
            String result = HttpRequest.post(url)
                    .header(Header.CONTENT_TYPE, "application/json;charset=utf-8")
                    .body(jsonStr)
                    .timeout(20000)//超时，毫秒
                    .execute().body();

            SRet bean = JSONUtil.toBean(result, SRet.class);

            if (bean.getCode().equals(HttpStatus.SUCCESS.getCode())) {
                JSONObject obj = (JSONObject)bean.getData();
                accountId = (Integer)(obj.get("id"));
            } else {
                return SRet.error(bean.getCode(), bean.getMsg());
            }
        } catch (Exception e) {
            throw new VerifyAccTokenErrorException();
        }

        // 获取到了账号id
        SRet info = playerService.getInfo(accountId);
        return info;
    }

    @PostMapping("/getCacheInfo")
    public SRet getCacheInfo(HttpServletRequest request){
        Integer playerId = GameJwtUtils.getPlayerIdByRequest(request);
        PlayerInfo playerInfo = SpringUtil.getBean(PlayerCache.class).getPlayerInfo(playerId);
        SGetInfo sGetInfo = playerService.getCacheInfo(playerInfo);
        return SRet.success(sGetInfo);
    }

    @PostMapping("/cfg")
    public SRet cfg(HttpServletRequest request){
        Integer maxHangTime = SpringUtil.getBean(GlobalConfig.class).getIntValue(GlobalConfig.HANG_TIME_MAX);
        SCfg msg = new SCfg();
        msg.setHangTimeMax(maxHangTime);
        System.out.println("aktest get cfg ok");
        return SRet.success(msg);
    }

    @PostMapping("/heartBeat")
    public SRet heartBeat(HttpServletRequest request){
        Integer playerId = GameJwtUtils.getPlayerIdByRequest(request);
        playerService.procHeartBeat(playerId);
        return SRet.success();
    }

//    @PostMapping("/passGuanQia")
//    public SRet passGuanQia(HttpServletRequest request) {
//        Integer playerId = GameJwtUtils.getPlayerIdByRequest(request);
//        playerService.passGuanQia(playerId);
//        return SRet.success();
//    }

    @PostMapping("/getHangAward")
    public SRet getHangAward(HttpServletRequest request) {
        Integer playerId = GameJwtUtils.getPlayerIdByRequest(request);
        SGetHangAward msg = playerService.getHangAward(playerId);
        return SRet.success(msg);
    }

    @Data
    public static class CTestExp {
        private int exp;
    }

    @PostMapping("/testExp")
    public SRet testExp(HttpServletRequest request, @RequestBody CTestExp msg) {
        Integer playerId = GameJwtUtils.getPlayerIdByRequest(request);
        playerService.addExp(playerId, msg.getExp());
        return SRet.success();
    }

    @PostMapping("/setLineup")
    public SRet setLineup(HttpServletRequest request, @RequestBody CSetLineup msg) {
        Integer playerId = GameJwtUtils.getPlayerIdByRequest(request);
        playerService.setLineup(playerId, msg.getHeroIds());
        return SRet.success();
    }
}
